package com.splant.android.smart.DALLevel;

import android.content.Context;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by JimOPC on 15-2-12.
 */
public class XmlFileManager {
    private Context currentContext;
    public XmlFileManager(Context context)
    {
        currentContext=context;
    }

    public HashMap<String,String> readXmlFile(String fileName)
    {
        try
        {
            HashMap<String,String> map=new HashMap<String, String>();

            InputStream inputStream=currentContext.openFileInput(fileName);
            XmlPullParser parser = Xml.newPullParser();       //利用Android的Xml工具类获取xmlPull解析器
            parser.setInput(inputStream, "UTF-8");                 //解析文件，设置字符
            int  event = parser.getEventType();//获取解析状态，返回的是int型数字状态

            while(event != XmlPullParser.END_DOCUMENT)
            { //如果状态不是结束事件END_DOCUMENT，就递归

                switch (event) {

                    case XmlPullParser.START_TAG:         //如果为开始解析属性START_TAG，则获取数据
                        if (parser.getDepth()>1)
                        {
                            String  arrtKeyName=parser.getName();
                            String  valueStr=parser.nextText();
                            map.put(arrtKeyName,valueStr);
                            break;
                        }

                }
                event = parser.next();   //让指针指向下一个节点
            }
            inputStream.close();
            return map;
        }
        catch (Exception e)
        {
              return null;
        }

    }

    public boolean writeXmlFile(String fileName,String groupName,HashMap<String,String> map)
    {
        boolean tf=false;
        try
        {
            StringWriter stringWriter = new StringWriter();
            XmlSerializer serializer = Xml.newSerializer();  //创建xml的序列化文件
            serializer.setOutput(stringWriter);
            serializer.startDocument("UTF-8", true);       //xml头标签字符集设置，是否序列化

            serializer.startTag(null,groupName);
            Set entrySet = map.entrySet();
            Iterator iterator = entrySet.iterator();
            while (iterator.hasNext())
            {
                Map.Entry keyValue = (Map.Entry) iterator.next();
                String key = keyValue.getKey().toString();
                String value = keyValue.getValue().toString();

                serializer.startTag(null, key);
                serializer.text(value);
                serializer.endTag(null, key);
            }
            serializer.endTag(null, groupName);

            serializer.endDocument(); //结束xml编辑，即设置xml文档结尾
            FileOutputStream outStream = currentContext.openFileOutput(fileName, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
            outStream.write(stringWriter.toString().getBytes());
            outStream.close();

            tf=true;
        }
        catch (Exception e)
        {
           tf=false;
        }
        return  tf;
    }


}
